package com.charlie.map;

import org.jboss.logging.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Vitaliy Mostipaka <imprtat@gmail.com>
 */
public class DirectionParserHandler extends DefaultHandler {

    private int step;
    private int distance;

    public DirectionParserHandler() {
        step = 0;
        distance = -1;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        switch (step) {
            case 0:
                if (qName.equals("distance")) {
                    step = 1;
                }
                break;
            case 1:
                if (qName.equals("value")) {
                    step = 2;
                }
                break;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (step == 2) {
            try {
                distance = Integer.parseInt(new String(ch, start, length));
            } catch (NumberFormatException ex) {
                Logger.getLogger(DirectionParserHandler.class.getName()).log(Logger.Level.FATAL, null, ex);
            }
            step = 0;
        }
    }

    public int getDistance() {
        return distance;
    }
}
